
<template>
  <div style="overflow-y: auto; margin: 15px">
    <h1>{{ id ? "编辑" : "新建" }}英雄</h1>
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
      style="margin-top: 30px"
      @submit.native.prevent
    >
      <el-tabs type="border-card" v-model="activeName">
        <el-tab-pane label="基础信息" name="basic">
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="称号" prop="title">
            <el-input v-model="ruleForm.title"></el-input>
          </el-form-item>
          <el-form-item label="头像" prop="avatar">
            <el-upload
              class="avatar-uploader"
              :action="uploadsUrl"
              :headers="getAuthHeaders()"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img
                v-if="ruleForm.avatar"
                :src="ruleForm.avatar"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="类型" prop="categories">
            <el-select v-model="ruleForm.categories" multiple>
              <el-option
                v-for="item of categories"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="难度" prop="difficult">
            <el-rate
              style="margin-top: 10px"
              :max="5"
              show-score
              v-model="ruleForm.scores.difficult"
            ></el-rate>
          </el-form-item>
          <el-form-item label="技能" prop="skills">
            <el-rate
              style="margin-top: 10px"
              :max="5"
              show-score
              v-model="ruleForm.scores.skills"
            ></el-rate>
          </el-form-item>
          <el-form-item label="攻击" prop="attack">
            <el-rate
              style="margin-top: 10px"
              :max="5"
              show-score
              v-model="ruleForm.scores.attack"
            ></el-rate>
          </el-form-item>
          <el-form-item label="生存" prop="survive">
            <el-rate
              style="margin-top: 10px"
              :max="5"
              show-score
              v-model="ruleForm.scores.survive"
            ></el-rate>
          </el-form-item>
          <el-form-item label="顺风出装" prop="items">
            <el-select v-model="ruleForm.items1" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="逆风出装" prop="items">
            <el-select v-model="ruleForm.items2" multiple>
              <el-option
                v-for="item of items"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="使用技巧" prop="usegeTips">
            <el-input type="textarea" v-model="ruleForm.usegeTips"></el-input>
          </el-form-item>
          <el-form-item label="对抗技巧" prop="battleTips">
            <el-input type="textarea" v-model="ruleForm.battleTips"></el-input>
          </el-form-item>
          <el-form-item label="团战思路" prop="teamTips">
            <el-input type="textarea" v-model="ruleForm.teamTips"></el-input>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能" name="skills">
          <el-button
            @click="ruleForm.skills.push({})"
            style="margin-bottom: 20px"
          >
            <i class="el-icon-plus"></i>
            添加技能
          </el-button>
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col
              :md="12"
              v-for="(item, index) of ruleForm.skills"
              :key="index"
            >
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="uploadsUrl"
                  :headers="getAuthHeaders()"
                  :show-file-list="false"
                  :on-success="(res) => $set(item, 'icon', res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item>
              <el-form-item label="描述">
                <el-input v-model="item.description" type="textarea"></el-input>
              </el-form-item>
              <el-form-item label="小提示">
                <el-input v-model="item.tips" type="textarea"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  @click="ruleForm.skills.splice(index, 1)"
                >
                  删除
                </el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

      <el-form-item style="margin-top: 20px">
        <el-button type="primary" @click="submitForm('ruleForm')">
          保存
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  props: ["id"],
  data() {
    return {
      activeName: "basic",
      ruleForm: {
        name: "",
        avatar: "",
        title: "",
        categories: "",
        scores: {
          difficult: 0,
        },
        skills: [],
      },
      categories: [],
      items: [],
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.fetchCategories();
    this.fetchItems();
    this.fetch();
  },
  methods: {
    fetchCategories() {
      this.$http.get("rest/categories").then((res) => {
        if (res.status === 200) {
          this.categories = res.data;
        }
      });
    },
    fetchItems() {
      this.$http.get("rest/items").then((res) => {
        if (res.status === 200) {
          this.items = res.data;
        }
      });
    },
    fetch() {
      this.id &&
        this.$http.get("rest/heroes/" + this.id).then((res) => {
          if (res.status === 200) {
            this.ruleForm = Object.assign({}, this.ruleForm, res.data);
          }
        });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$http
            .post(
              "rest/heroes" + (this.id ? `/${this.id}` : ""),
              this.id ? { id: this.id, ...this.ruleForm } : this.ruleForm
            )
            .then((res) => {
              if (res.status === 200) {
                this.$router.push("/heroes/list");
                this.$message({
                  type: "success",
                  message: "保存成功",
                });
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleAvatarSuccess(res) {
      this.ruleForm.avatar = res.url;
      // URL.createObjectURL(file.raw);
    },
  },
};
</script>


<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  line-height: 80px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
</style>
